package com.example.com.transcribeeditpublish.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.databinding.ActivityOutlinkBinding;
import com.example.com.transcribeeditpublish.utils.DialogUtil;
import com.example.com.transcribeeditpublish.utils.FileUtils;
import com.example.com.transcribeeditpublish.utils.TCConstants;
import com.example.com.transcribeeditpublish.utils.TCEditerUtil;
import com.example.com.transcribeeditpublish.utils.down.DownloadUtil;

import java.io.File;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.BaseUrlExternalStrageDir;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

import com.example.com.transcribeeditpublish.BR;

/**
 * Created by i7-1 on 2018/12/11.
 */

public class OutLinkActivity extends BaseActivity<ActivityOutlinkBinding, BaseViewModel> {

    private EditText linkUrl;
    private String downFile;


    @Override
    public int initContentView() {
        return R.layout.activity_outlink;
    }

    @Override
    public int initVariableId() {
        return BR.entity;
    }

    @Override
    public BaseViewModel initViewModel() {
        return null;
    }

    @Override
    public void initParam() {
        super.initParam();
        ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData data = cm.getPrimaryClip();
        ClipData.Item item = data.getItemAt(0);
        final String content = item.getText().toString();
        final MaterialDialog.Builder builder = MaterialDialogUtils.showBasicDialogNoTitle(this, "检测到剪切板上有站外链接，是否解析?");
        MaterialDialog dialog = builder.build();
        builder.onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                getBinding().linkUrl.setText(content);
                dialog.dismiss();
                analyseUrl();
            }
        });
        builder.onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    @Override
    public void initData() {
        String folder = BaseUrlExternalStrageDir.getBaseUrl() + File.separator + "video";
        File file = new File(folder);
        if (!file.exists()) {
            file.mkdirs();
        }

        downFile = folder + File.separator + "link.mp4";
        linkUrl = (EditText) findViewById(R.id.link_url);
        TextView textView = (TextView) findViewById(R.id.r_text);
        textView.setText("下一步");
        textView.setVisibility(View.VISIBLE);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                analyseUrl();
            }
        });
    }


    private void analyseUrl() {
        String url = linkUrl.getText().toString();

        if (StringUtils.isEmpty(url)) {
            ToastUtils.showShort("请填写链接");
            return;
        }
        showDialog("解析中...");
        showDialog("下载中...");
        DownloadUtil.get().download(url, downFile, new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess() {
                KLog.e("onDownloadSuccess");
                toSimpleEdit();
            }

            @Override
            public void onDownloading(int progress) {
                KLog.e("progress=" + progress);
            }

            @Override
            public void onDownloadFailed() {
                KLog.e("onDownloadFailed");
                dismissDialog();
            }
        });

    }

    private void toSimpleEdit() {
        Intent intent = new Intent(this, VideoSimpleActivity.class);
        intent.putExtra(TCConstants.VIDEO_EDITER_PATH, downFile);
        startActivity(intent);
        dismissDialog();
    }
}
